<?php
//error_reporting(E_ALL);
//ini_set('display_errors','1');


class campaigns extends CI_Controller
{
    private $dir = 'EmailMarketing';
    function __construct()
    {
        
        parent::__construct();
        $this->is_login(2);
        $this->load->library('ion_auth');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->library('pagination');
        $this->load->database();
        $this->lang->load('auth');
        $this->lang->load('admin_estate');
        $this->load->helper('language');
        $this->load->model('admin_estate_model');
        
        $this->load->helper('email');
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->helper('string');
        
        $this->load->model('mod_emailmarketing');
        $this->load->model('mod_users');
        $this->load->model('emailgroup_model');
        $this->load->model('codegen_model', '', TRUE);
        $this->load->model('list_model');
        $this->load->model('campaign_model');
        $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
        $this->form_validation->set_message('required', '%s cannot be blank');
    }
    
    /**
     * Loads Email settings page.
     *
     * @access	private
     * @param	nil
     * @return	void
     */
    public function is_login($level = 2)
    {
        if (!$this->ion_auth->logged_in()) {
            //redirect them to the login page
            redirect('login', 'refresh');
        } elseif (!$this->ion_auth->is_admin($level)) {
            $this->load->helper('admin');
            redirect(site_url(), 'refresh');
        }
    }
    function index()
    {
        $data['message']   = $this->session->flashdata('message');
        $data['dir']       = $this->dir;
        $data['campaigns'] = $this->db->get('mw_campaign')->result();
        $this->tmadmin->tmView('email_marketing/campaign_index', $data);
    }
    
    private function create()
    {
        
        $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
        $this->form_validation->set_message('required', '%s cannot be blank');
        $data['custom_error'] = '';
        
        if ($this->form_validation->run('mw_campaign') == false) {
        } else {
            $list = $this->list_model->get_list_defaults($this->input->post('list_id'));
            $data = array(
                'campaign_uid' => $this->codegen_model->get_random_uid_string(),
                'customer_id' => $this->session->userdata('user_id'),
                'list_id' => $this->input->post('list_id'),
                'segment_id' => $this->input->post('segment_id'),
                'name' => $this->input->post('name'),
                'from_name' => $list['from_name'],
                'from_email' => $list['from_email'],
                'to_name' => set_value('to_name'),
                'reply_to' => $list['reply_to'],
                'subject' => $list['subject'],
                'date_added' => $this->codegen_model->get_date(),
                'last_updated' => $this->codegen_model->get_date()
            );
            
            if ($this->codegen_model->add('mw_campaign', $data) == TRUE) {
                $campaign_id      = $this->db->insert_id();
                //Inserting Campaing Options
                $campaign_options = array(
                    'campaign_id' => $campaign_id
                );
                $this->codegen_model->add('mw_campaign_option', $campaign_options);
                //
                //Inserting Campaing Template Information
                $campaign_template = array(
                    'campaign_id' => $campaign_id,
                    'content' => '<html>
<head>
	<title></title>
</head>
<body> </body>
</html>'
                );
                
                $this->codegen_model->add('mw_campaign_template', $campaign_template);
                $this->session->set_flashdata('message', 'Your form has been successfully saved!');
                redirect('email_marketing/campaigns/setup/' . $data['campaign_uid']);
                
            }
            
            
        }
        $this->load->model('list_model');
        $data['subscribers_lists'] = $this->list_model->get_list_subscribers();
        $this->tmadmin->tmView('email_marketing/campaign_create', $data);
        
    }
    public function _remap($method, $parameter)
    {
        if (method_exists($this, $method)) {
            $this->$method($parameter[0]);
        }
        
        
    }
    
    private function setup($campaign_uid)
    {
        if ($this->input->post('submit')) {
            
            if ($this->form_validation->run('mw_campaign_setup') == FALSE) {
                $data['custom_error'] = "Your form has a few errors, please fix them and try again!";
                
            } else {
                $data_campaign_option = array(
                    'url_tracking' => set_value('url_tracking'),
                    'json_feed' => set_value('json_feed'),
                    'xml_feed' => set_value('xml_feed'),
                    'plain_text_email' => set_value('plain_text_email'),
                    'email_stats' => set_value('email_stats')
                );
                $data_campaign        = array(
                    'from_name' => set_value('from_name'),
                    'from_email' => set_value('from_email'),
                    'to_name' => set_value('to_name'),
                    'reply_to' => set_value('reply_to'),
                    'subject' => set_value('subject'),
                    'last_updated' => $this->codegen_model->get_date()
                );
                $campaign             = $this->db->get_where('mw_campaign', array(
                    'campaign_uid' => $campaign_uid
                ))->row_array();
                
                if ($this->codegen_model->edit('mw_campaign', $data_campaign, 'campaign_id', $campaign['campaign_id']) == TRUE) {
                    if ($this->codegen_model->edit('mw_campaign_option', $data_campaign_option, 'campaign_id', $campaign['campaign_id']) == TRUE) {
                        $this->session->set_flashdata('message', 'Your form has been successfully saved!');
                        redirect("email_marketing/campaigns/template/" . $campaign_uid);
                    }
                    
                }
            }
            
        }
        
        $data['campaign'] = $this->campaign_model->get_campaign_options($campaign_uid);
        $this->tmadmin->tmView('email_marketing/campaign_setup', $data);
        
        
    }
    private function delete($campaign_uid)
    {
		 $campaign = $this->db->get_where('mw_campaign', array(
            'campaign_uid' => $campaign_uid
        ))->row_array();
		
		$this->codegen_model->delete('mw_campaign_template','campaign_id',$campaign['campaign_id']);
		$this->codegen_model->delete('mw_campaign_option','campaign_id',$campaign['campaign_id']);
		$this->codegen_model->delete('mw_campaign','campaign_id',$campaign['campaign_id']);
		
        $this->session->set_flashdata('message', 'Campaign is Deleted Successfully');
        redirect("email_marketing/campaigns");
        
        
    }
    private function template($campaign_uid)
    {
        $campaign = $this->db->get_where('mw_campaign', array(
            'campaign_uid' => $campaign_uid
        ))->row_array();
        if ($this->input->post('submit')) {
            
            if ($this->form_validation->run('mw_campaign_template') == false) {
                $data['custom_error'] = "Your form has a few errors, please fix them and try again!";
                
            } else {
                
                $data = array(
                    'content' => $this->input->post('content'),
                    'inline_css' => $this->input->post('inline_css'),
                    'plain_text' => $this->input->post('plain_text'),
                    'auto_plain_text' => $this->input->post('auto_plain_text')
                );
                
                
                if ($this->codegen_model->edit('mw_campaign_template', $data, 'campaign_id', $campaign['campaign_id']) == TRUE) {
                    $this->session->set_flashdata('message', 'Your form has been successfully saved!');
                    redirect("email_marketing/campaigns/template/" . $campaign_uid);
                }
                
            }
        }
        
        $data['campaign_template'] = $this->db->get_where('mw_campaign_template', array(
            'campaign_id' => $campaign['campaign_id']
        ))->row_array();
        $this->tmadmin->tmView('email_marketing/campaign_template', $data);
        
        //print_r($this->db->get('mw_campaign_template')->result());
        
    }
    
    
    
}
